package pres.lnk.spring_boot_starter_hello;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 基于SpringBoot自动注入配置文件属性来开发程序
 * 可用来开发SpringBoot插件之类的
 * 本例核心是文件src/main/resources/META-INF/spring.factories
 * spring.factories是用来指定本项目的配置并添加到Spring容器中
 * @author linnankun
 * @date 2017年4月7日
 */
@Configuration	//将该类设为配置类
//启动HelloServiceProperties自动注入资源文件到属性
@EnableConfigurationProperties(HelloServiceProperties.class)
//当HelloService类存在情况下条件符合，配置生效
@ConditionalOnClass(HelloService.class)
//当资源文件的属性hello=enabled则条件符合，matchIfMissing为如果没有设置hello默认为条件符合
@ConditionalOnProperty(prefix="hello", value="enabled", matchIfMissing = true)
public class HelloServiceAutoConfiguration {
	@Autowired
	private HelloServiceProperties helloServiceProperties;
	
	@Bean
	@ConditionalOnMissingBean(HelloService.class)
	public HelloService helloService(){
		HelloService helloService = new HelloService();
		helloService.setMsg(helloServiceProperties.getMsg());
		return helloService;
	}
}
